﻿using EntityPet;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PET.Tools;

namespace PET.Controllers
{
    /// <summary>
    /// 宠物护理数据管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class CareController : ControllerBase
    {
        private readonly PetDbContext db;
        public CareController(PetDbContext _context)
        {
            db = _context;

        }

        //查询所有用户护理数据
        [HttpGet]
        public Result Get()
        {
            Result result = new Result();
            result.Data= db.Pet_Care.ToList();
            return result;
        }

        //根据id查询信息
        [HttpGet("{id}")]
        public Result GetId(Guid id)
        {
            Result result = new Result();
            result.Data = db.Pet_Care.Where(w => w.Id == id).SingleOrDefault();
            return result;
        }

        //用户数据添加
        [HttpPost]
        public Result AddPet([FromBody] Pet_Care care)
        {
            Result result = new Result();
            result.Data = db.Pet_Care.Add(care);
            result.Data = db.SaveChanges();
            return result;
        }

        //用户数据更改
        [HttpPut]
        public Result Put([FromBody] Pet_Care care)
        {
            Result result = new Result();
            result.Data = db.Pet_Care.Update(care);
            result.Data = db.SaveChanges();
            return result;
        }

        //用户数据删除(信息暂存)
        [HttpDelete("{id}")]
        public Result Del(Guid id)
        {
            Result result = new Result();
            Pet_Care? care = new Pet_Care();
            care=db.Pet_Care.Where(s => s.Id == id).FirstOrDefault();
            if (care != null)
            {
                care.DelTime = DateTime.Now;
                db.Pet_Care.Update(care);
                result.Data = db.SaveChanges();
            }
            return result;
        }
    }
}
